<template>
	<view class="index">
		<image src="../../../static/images/load.gif" mode="widthFix" class="logo"></image>
		<view class="text">
			支付中
		</view>
	</view>
</template>

<script>
	let app = getApp()
	const util = require("@/utils/util.js")
	export default {
		data() {
			return {
				payType: "", // 支付类型
				orderSn: "", // 订单编号
				orderId: "", // 订单id
				timer: null, // 定时器
			}
		},
		methods: {
			// 设置定时器
			setTimer() {
				let this
				if (this.timer == null) {
					this.timer = setInterval(() => {
						console.log('开始定时...每过一秒执行一次')
						util.request(`order/getOrderPaymentStatus/${this.orderSn}`).then((res)=> {
							if (res.code == 0) {
								if (res.data == "paySuccess") {
									uni.redirectTo({
										url: '/pages/payResult/payResult?status=1&orderId=' +
											this.orderId
									});
								} else {
									uni.redirectTo({
										url: '/pages/payResult/payResult?status=0&orderId=' +
											this.orderId + '&msg=付款失败请重新付款~~~'
									});
								}
							}
						});
					}, 1000)
				}
			},
			// 跳转外部小程序支付
			gotoPay() {
				let that = this;
				if (that.payType == 'alipay') {
					// 支付宝支付
					uni.getSystemInfo({
						success(res) {
							if (res.platform == 'android') {
								const payinfos = '?orderSn=' + that.orderSn + '&orderId=' + that.orderId;
								const query = encodeURIComponent(payinfos);
								plus.runtime.openURL(
									"alipays://platformapi/startapp?appId=2021003159693605&page=pages/index/index" +
									query);
							} else {
								const payinfos = '?orderSn=' + that.orderSn + '&orderId=' + that.orderId;
								const query = encodeURIComponent(payinfos);
								plus.runtime.openURL(
									"alipay://platformapi/startapp?appId=2021003159693605&page=pages/index/index" +
									query);
							}
						}
					});
				} else if (that.payType == 'wxPay') {
					// 微信支付
					plus.share.getServices(function(wxRes) {
						var sweixin = null;
						for (var i = 0; i < wxRes.length; i++) {
							var t = wxRes[i];
							if (t.id == 'weixin') {
								sweixin = t;
							}
						}
						if (sweixin) {
							sweixin.launchMiniProgram({
								id: 'gh_e3bda047d7f9',
								path: 'pages/wxPay/wxPay?orderSn=' + that.orderSn +
									'&orderId=' + that.orderId,
								type: 2
							});
						}
					}, function(err) {
						console.log(JSON.stringify(err));
					});
				}
			}
		},
		onLoad(options) {
			/**
			 * 1.拿到参数判断支付宝或微信
			 * 2.做跳转对应小程序
			 * 3.设置定时器，定时刷新当前订单支付状态
			 * 4.支付状态成功取消定时器并跳转支付结果页面
			 */
			if (options.payType) {
				this.payType = options.payType
				this.orderSn = options.orderSn
				this.orderId = options.orderId

				clearInterval(this.timer)
				this.timer = null
				this.setTimer()
			}
		},
		destroyed() {
			// 每次离开当前界面时，清除定时器
			clearInterval(this.timer)
			this.timer = null
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #f9f9f9;
	}

	.index {
		width: 100%;

		.logo {
			width: 350rpx;
			margin: 350rpx auto 20rpx;
			display: block;
		}
		.text {
			width: 100%;
			text-align: center;
			font-size: 40rpx;
			color: #333;
		}
	}
</style>
